<template>
  <div>
    <div class="buttons">
      <div class="buttonGroup1">
        <el-input style="width: 200px; margin-right: 5px" placeholder="输入级别" v-model="selectPage.level"></el-input>
        <el-select v-model="value" placeholder="选择指标" style="margin-right: 5px">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
        <el-button type="primary" @click="load(1)">查询</el-button>
        <el-button type="info" @click="reset">重置</el-button>
      </div>
      <div class="buttonGroup2">
        <el-button type="primary" @click="addTempVisible = true">创建模板</el-button>
        <el-button type="primary" @click="addVisible = true">添加一级指标</el-button>
      </div>
    </div>

    <el-table :data="tableData" stripe :header-cell-style="{backgroundColor: 'aliceblue', color: '#666'}"
              style="margin-top: 10px">
      <el-table-column prop="id" label="序号" width="50" align="center"></el-table-column>
      <el-table-column prop="parentContent" label="父级" width="200" align="center"></el-table-column>
      <el-table-column prop="content" label="内容" align="center"></el-table-column>

      <el-table-column label="操作" align="center" width="180">
        <template v-slot="scope">
          <el-button size="mini" type="primary" plain @click="handleEdit(scope.row)">添加</el-button>
          <el-button size="mini" type="danger" plain @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="selectPage.pageNum"
          :page-size="selectPage.pageSize"
          layout="total, prev, pager, next"
          :hide-on-single-page="hideValue"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="创建模板" :visible.sync="addTempVisible">
      <el-form ref="addUserTable">
        <el-form-item label="内容">
          <el-input type="textarea" placeholder="请输入模板名称" v-model="templates.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addTempVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTemplate">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加一级指标" :visible.sync="addVisible">
      <el-form ref="addUserTable">
        <el-form-item label="内容">
          <el-input type="textarea" placeholder="请输入内容" v-model="criterion.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCriterion">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="添加子指标" :visible.sync="addSonVisible">
      <el-form ref="addUserTable">
        <el-form-item label="内容">
          <el-input type="textarea" placeholder="请输入内容" v-model="criterion.content"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addSonVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCriterion">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/urils/request";

export default {
  name: "CriterionUpdate",
  data() {
    return {
      total: 0,
      hideValue: false,
      tableData: [],
      selectPage: {
        pageNum: 1,
        pageSize: 11,
        level: '',
        templateId: ''
      },
      addVisible: false,
      addSonVisible: false,
      addTempVisible: false,
      criterion: {
        content: '',
        parentContent: '',
        parent: '',
        level: 0,
        templateId: '',
      },
      templates: {
        name: ''
      },
      value: '',
      options: []
    }
  },
  methods: {
    reset() {
      this.selectPage.level = ''
      this.load()
    },
    resetClass() {
      this.criterion.content = ''
      this.criterion.parentContent = ''
      this.criterion.parent = ''
      this.criterion.level = ''
      this.criterion.templateId = ''
    },
    load(pageNum) {
      if (pageNum) this.selectPage.pageNum = pageNum
      this.selectPage.templateId = this.value
      request.post('/criterion/selectPage', this.selectPage).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    addCriterion() {
      if (this.value === '') {
        this.$message.error("请先选择指标")
        this.addVisible = false
        return false
      }else{
        this.criterion.templateId = this.value
      }
      if (this.criterion.content === '') {
        this.addVisible = false
        return false
      } else {
        if (this.criterion.level === 0) {
          this.criterion.level = 1
        }
        console.log(this.criterion)
        request.post('/criterion/add', this.criterion).then(res => {
          if (res.code === '200') {
            this.$message.success('添加成功')
            this.resetClass()
            this.load(this.selectPage.pageNum)
            this.addVisible = false
            this.addSonVisible = false
          } else {
            this.$message.error(res.msg)
          }
        })
      }
    },
    addTemplate(){
      if(this.templates.name === ''){
        this.$message.error("请输入模板名称")
        return false
      }
      request.post('/criterion/addTemplate',this.templates).then(res => {
        if(res.code === '200'){
          this.$message.success('创建成功')
          this.addTempVisible = false
          this.loadTemplate()
        } else{
           this.$message.error(res.msg)
        }
      })
    },
    handleEdit(row) {   // 编辑数据
      this.criterion = JSON.parse(JSON.stringify(row))  // 给form对象赋值  注意要深拷贝数据
      this.criterion.content = ''
      this.criterion.level = row.level + 1
      this.criterion.parent = row.id
      this.criterion.parentContent = row.content
      this.addSonVisible = true
      // console.log(this.criterion)
    },
    del(id) {
      this.$confirm('是否确认删除该数据', '确认删除', {type: "warning"}).then(response => {
        request.delete('/criterion/delete/' + id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('删除成功')
            // location.reload()
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    loadTemplate() {
      this.options = []
      request.get('/criterion/templateList').then(res => {
        if (res.code === '200') {
          for (let i in res.data) {
            this.options.push({
              value: res.data[i].id,
              label: res.data[i].name
            })
          }
        }
      })
    }
  },
  created() {
    this.load()
    if (this.total === 0) {
      this.hideValue = true
    }
    this.loadTemplate()
  }
}
</script>

<style scoped>
.buttons {
  width: 100%;
  display: flex;
}

.buttonGroup1 {
  flex: 1;
}

.buttonGroup2 {
  /*margin-right: 40px;*/
  flex: 1;
  display: flex;
  justify-content: flex-end;
}
</style>